package com.example.stylecameraversion01;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;

import javax.crypto.spec.IvParameterSpec;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

public class ActivityCapture extends Activity {
	private static final int REQUEST_CODE = 1888;
	ImageView img_view, imgSave, imgShare;
	Bitmap photo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.layout_edit_picture);
		
		img_view = (ImageView) findViewById(R.id.imageFrame);
		
		imgSave = (ImageView) findViewById(R.id.btnSave);
		
		imgShare = (ImageView) findViewById(R.id.btnShare);
		
		capture();

		imgSave.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				SaveImage(photo);
			}
		});
		imgShare.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				ShareImage(photo);
			}
		});

	}

	private void SaveImage(Bitmap bitmap) {
		img_view.setDrawingCacheEnabled(true);
		bitmap = img_view.getDrawingCache();
		String root = Environment.getExternalStorageDirectory().toString();
		File newDir = new File(root + "/saved_images");
		newDir.mkdirs();
		Random gen = new Random();
		int n = 10000;
		n = gen.nextInt(n);
		String photoname = "Image-" + n + ".jpg";
		File file = new File(newDir, photoname);
		if (file.exists())
			file.delete();
		try {
			FileOutputStream out = new FileOutputStream(file);
			bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
			out.flush();
			out.close();
			Toast.makeText(getApplicationContext(), "Saved to your folder",
					Toast.LENGTH_SHORT).show();
		} catch (Exception e) {

		}
	}

//	private void ShareImage(Bitmap bitmap) {
//		BitmapDrawable bitmapDrawable = (BitmapDrawable) img_view
//				.getDrawable();
//		bitmap = bitmapDrawable.getBitmap();
//
//		// Save this bitmap to a file.
//		File cache = getApplicationContext().getExternalCacheDir();
//		File sharefile = new File(cache, "toshare.png");
//		try {
//			FileOutputStream out = new FileOutputStream(sharefile);
//			bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
//			out.flush();
//			out.close();
//		} catch (IOException e) {
//
//		}
//
//		// Now send it out to share
//		Intent share = new Intent(android.content.Intent.ACTION_SEND);
//		share.setType("image/*");
//		share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + sharefile));
//		try {
//			startActivity(Intent.createChooser(share, "Share photo"));
//		} catch (Exception e) {
//
//		}
//	}

	public void capture() {
		Intent cameraIntents = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		// fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
		startActivityForResult(cameraIntents, REQUEST_CODE);
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
			photo = (Bitmap) data.getExtras().get("data");
			img_view.setImageBitmap(photo);
		}
	}

	// private void SaveImage(Bitmap finalBitmap) {
	//
	// String root = Environment.getExternalStorageDirectory().toString();
	// File myDir = new File(root + "/saved_images");
	// myDir.mkdirs();
	// Random generator = new Random();
	// int n = 10000;
	// n = generator.nextInt(n);
	// String fname = "Image-" + n + ".jpg";
	// File file = new File(myDir, fname);
	// if (file.exists())
	// file.delete();
	// try {
	// FileOutputStream out = new FileOutputStream(file);
	// finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
	// out.flush();
	// out.close();
	//
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	//
	// }

}
